package com.iqismart.shorturl.service.impl;

import com.iqismart.shorturl.config.UidHolder;
import com.iqismart.shorturl.exception.BaseException;
import com.iqismart.shorturl.model.EntityReport;
import com.iqismart.shorturl.repository.ReportRepository;
import com.iqismart.shorturl.service.ReportService;
import com.iqismart.shorturl.vo.ReportRequest;
import com.iqismart.shorturl.vo.ReportResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description
 * @Auther sty
 * @createTime 2019-10-15 15:19
 */
@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportRepository reportRepository;
    @Override
    public ReportResponse add(ReportRequest request) {

        EntityReport entityReport = new EntityReport();
        if(StringUtils.isNotEmpty(request.getUrl())) {
            entityReport.setUrl(request.getUrl());
        }else{
            BaseException.failAndThrow("请输入链接");
        }
        if(StringUtils.isNotEmpty(request.getName())) {
            entityReport.setName(request.getName());
        }
        if(StringUtils.isNotEmpty(request.getContact())) {
            entityReport.setContact(request.getContact());
        }

        entityReport.setUid(UidHolder.getUid());
        entityReport = reportRepository.save(entityReport);

        final ReportResponse response = new ReportResponse();
        BeanUtils.copyProperties(entityReport,response);

        return response;
    }
}
